using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.FrameXml.Design.Services.NameCreationService;
using Microsoft.WowAddonStudio.Tests.Common.Mocks;

using NUnit.Framework;

namespace Microsoft.WowAddonStudio.FrameXml.Design.Tests
{
	[TestFixture]
	public class FrameXmlNameCreationServiceTest
	{
		#region SetUp

		[SetUp]
		public void SetUp()
		{
		}

		#endregion

		#region TearDown

		[TearDown]
		public void TearDown()
		{
			SimpleTestServiceLocator.Reset();
		}

		#endregion

		#region Tests

		[Test(Description = "")]
		public void Test_GetNumericChars()
		{
			int value = UniqueNumberGeneratorBase.GetNumericChars("1524");
			Console.WriteLine(value);
			Assert.AreEqual(value, 1524);

			value = UniqueNumberGeneratorBase.GetNumericChars("152b4");
			Console.WriteLine(value);
			Assert.AreEqual(value, 152);

			value = UniqueNumberGeneratorBase.GetNumericChars("b152b4");
			Console.WriteLine(value);
			Assert.AreEqual(value, 1);

			value = UniqueNumberGeneratorBase.GetNumericChars(string.Empty);
			Console.WriteLine(value);
			Assert.AreEqual(value, 1);
		}


		[Test(Description = "")]
		public void Test_GenerateSimpleUniqueNumber()
		{
			var locator = new SimpleTestServiceLocator();
			UniqueNumberGeneratorBase generator = new SimpleUniqueNumberGenerator(locator);

			locator.Add(new Button {Name = "Button1"});
			int number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(2, number);

			locator.Add(new Button { Name = "Button2" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(3, number);

			locator.Add(new Button { Name = "Button10" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(11, number);

			locator.Add(new Button { Name = "Button15467" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(15468, number);

			number = generator.GenerateUniqueNumber("List");
			Console.WriteLine(number); Assert.AreEqual(1, number);

			locator.Add(new Button { Name = "Button174b67" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(15468, number);
		}

		[Test(Description = "")]
		public void Test_GenerateContinualUniqueNumber()
		{
			var locator = new SimpleTestServiceLocator();
			UniqueNumberGeneratorBase generator = new ContinualUniqueNumberGenerator(locator);

			locator.Add(new Button {Name = "Button1"});
			int number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number);
			Assert.AreEqual(2, number);

			locator.Add(new Button { Name = "Button2" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(3, number);

			locator.Add(new Button { Name = "Button10" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(3, number);

			locator.Add(new Button { Name = "Button4" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(3, number);

			locator.Add(new Button { Name = "Button3" });
			number = generator.GenerateUniqueNumber("Button");
			Console.WriteLine(number); Assert.AreEqual(5, number);
		}

		[Test(Description = "")]
		public void Test_GetControls()
		{
			var locator = new SimpleTestServiceLocator();
			UniqueNumberGeneratorBase generator = new ContinualUniqueNumberGenerator(locator);
			locator.Add(new Button { Name = "Button1" });
			locator.Add(new Button { Name = "Button2" });

			var button = new Button {Name = "Button3"};
			button.Controls.Add(new Button { Name = "Button31" });

			var btnSub = new Button {Name = "Button32"};
			btnSub.Controls.Add(new Button { Name = "Button311" });

			button.Controls.Add(btnSub);
			locator.Add(button);
			locator.Add(new Button { Name = "Button4" });

			List<Component> controls = generator.GetComponents("Button");
			controls.ForEach(ctrl => Console.WriteLine(ctrl.Site.Name));
			Assert.AreEqual(7, controls.Count);
		}

		#endregion
	}
}